//
//  UserViewModel.swift
//  MVVMDemo1
//
//  Created by 李煜 on 2024/6/7.
//

import UIKit

struct User {
    var username : String
    var email    : String
}
class UserViewModel {
    var user: User?
    
    //网络请求类
    var userService = UserService()
    
    var username: String {
        return user?.username ?? ""
    }
    var email: String {
        return user?.email ?? ""
    }
    
    func fetchUser() {
        userService.fetchUser { [weak self] user in
            self?.user = user
            //通知视图更新数据
            DispatchQueue.main.async {
                self?.updateView?()
            }
        }
        
    }
    
    //回调给控制器更新UI
    var updateView: (() -> Void)?
}
